﻿/*
Copyright � Edward Nutting 2013
Author: Edward Nutting 
Date: Jul 8 18:31 2013

URL: https://typescriptui.codeplex.com/
Modifications:
 - 8/Jul/13 : Initial version.

License: https://typescriptui.codeplex.com/license
*/

/// <reference path="Classes/Control.ts" />

module TSUI.UI
{
    export var OpenWindows: number = 0;
    export var JustUsedTabKeyTime: number = 0;
    export var CurrentFocusedControl: IControl = null;
    export var _currTabIndex = 0;
    
    export var Global_MouseUpEvent: Events.MouseUpEvent = new Events.MouseUpEvent();
    export var Global_MouseMoveEvent: Events.MouseMoveEvent = new Events.MouseMoveEvent();

    export var preventTabKey: bool = false;
    export var OnKeyDown_Global_First = function (jqEvent: JQueryEventObject)
    {
        if (jqEvent.keyCode === 9)
        {
            JustUsedTabKeyTime = Date.now();
        }
        return true;
    };
    export var OnKeyDown_Global_Last = function (jqEvent: JQueryEventObject)
    {
        if (jqEvent.keyCode === 9)
        {
            if (preventTabKey)
            {
                preventTabKey = false;
                StopEvent(jqEvent);
                return false;
            }
        }

        return !jqEvent.isDefaultPrevented();
    };

    var _tapStartTime: number = Number.MAX_VALUE - 1000;
    var _tapTarget: JQuery = null;
    if (document.documentElement.hasOwnProperty('ontouchstart'))
    {
        $(document).on("touchstart", function (event: JQueryEventObject)
        {
            _tapStartTime = Date.now();
            _tapTarget = $(event.target);
        });
    }

    $(document).on(Control.OnMouseUpEventName, function (event: JQueryEventObject)
    {
        if (false && _tapStartTime + 10 < Date.now())
        {
           // _tapTarget.click();
            _tapTarget = null;
            _tapStartTime = -1;
        }
        else
        {
            event = standardiseEvent(event);

            Global_MouseUpEvent.Invoke(new Events.MouseUpEventArgs(null, event));

            if (!event.isPropagationStopped() && CurrentFocusedControl !== null &&
                !$(event.target).is("input"))
            {
                CurrentFocusedControl.Blur();
            }
        }
    });
    $(document).on(Control.OnMouseMoveEventName, function (event)
    {
        //if (_tapStartTime + 10 > Date.now())
        {
            event = standardiseEvent(event);
            Global_MouseMoveEvent.Invoke(new Events.MouseMoveEventArgs(null, event));
        }
    });
    
       
    $(document).on("keyup", function (event)
    {
        if (!event.isPropagationStopped() && CurrentFocusedControl !== null && event.keyCode === 27)
        {
            CurrentFocusedControl.Blur();
        }
    });
}
$(function ()
{
    $("body").append("<div class=\"Bg\">");
});